//
//  ZJZDTableMenuView.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/7/3.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJZDTableMenuView.h"
#import "ZJZDTableViewCell.h"
#define CELLID @"ZJZDTableViewCell.h"
@interface ZJZDTableMenuView ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
{
    
    ZJZDTableViewCell *_seleCell;
}
/** tableView */
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation ZJZDTableMenuView

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.frame = CGRectMake(0, 0, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - 49);
        UITapGestureRecognizer *tapGester = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(action_tapGester)];
        [self addGestureRecognizer:tapGester];
        tapGester.cancelsTouchesInView = NO;
        tapGester.delegate = self;
        [self addSubview:self.tableView];
    }
    return self;
}

- (void)action_tapGester{
    [self removeFromSuperview];
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    // 若为UITableViewCellContentView（即点击了tableViewCell），则不截获Touch事件
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }
    return  YES;
}

#pragma mark - UITableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJZDTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID forIndexPath:indexPath];
    if ([_seleIndex isEqualToString: [NSString stringWithFormat:@"%ld",(long)indexPath.row]]) {
        cell.contentView.backgroundColor = [[UIColor themeColor] colorWithAlphaComponent:0.5];
        _seleCell = cell;
    }else{
        cell.contentView.backgroundColor = [UIColor themeColor];
    }
    cell.titleLabel.text = self.dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40 * [UIScreen zj_screenHScale];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_seleCell) {
        _seleCell.contentView.backgroundColor = [UIColor themeColor];
    }
    ZJZDTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor zj_colorWithHexString:@"1768a9"];
    _seleIndex = [NSString stringWithFormat:@"%ld",indexPath.row];
    _seleCell = cell;
    
    //3、党建中心（默认） 4、跨省组织关系转接 5、党建服务站 6、组织生活开放点 //社区党校@“”
    NSString *typeString = @"";
    if ([cell.titleLabel.text isEqualToString:@"党建服务中心"]) {
        typeString = @"3";
    }else if ([cell.titleLabel.text isEqualToString:@"跨省市组织关系转接"]) {
        typeString = @"4";
    }else if ([cell.titleLabel.text isEqualToString:@"党建服务站"]) {
        typeString = @"5";
    }else if ([cell.titleLabel.text isEqualToString:@"组织生活开放点"]) {
        typeString = @"6";
    }
    if (self.delegate) {
        [self.delegate ZJZDTableMenuViewDelegate_cellClick_TypeString:typeString];
    }
    [self removeFromSuperview];
}



#pragma mark - Setter
- (void)setPoint:(CGPoint)point{
    _point = point;
    CGFloat tableViewWidth = 0;
    for (int i = 0; i < self.dataArray.count; i++) {
        CGFloat  width = [NSString zj_sizeOfString:self.dataArray[i] font:[UIFont titleTextFont] width:[UIScreen zj_screenWith]].width;
        tableViewWidth = tableViewWidth > width ? tableViewWidth: width;
    }
    
    self.tableView.width = tableViewWidth + 20 * [UIScreen zj_screenWScale];
    self.tableView.y = point.y;
    self.tableView.right = point.x - 12 * [UIScreen zj_screenWScale];
    self.tableView.height = 40 * [UIScreen zj_screenHScale] * self.dataArray.count;
    [self.tableView reloadData];
}
#pragma mark - Getters

- (UITableView *)tableView{
    if (_tableView == nil){
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 150 * [UIScreen zj_screenWScale], 4 * 40 * [UIScreen zj_screenHScale]) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = NO;
        _tableView.layer.cornerRadius = 5 *[UIScreen zj_screenWScale];
        [_tableView registerClass:[ZJZDTableViewCell class] forCellReuseIdentifier:CELLID];
    }
    return _tableView;
}

@end
